//
//  SneakersController.m
//  WANNA
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 浮夸. All rights reserved.
//

#import "SneakersController.h"
#import "shoseModel.h"
#import "shoseCell.h"
#import "LDTNetWorkRequestManager.h"
#import "LDTCycleView.h"
#import "shoseDetialController.h"
@interface SneakersController ()
@property (nonatomic,strong)LDTCycleView *cycleView;
@property (nonatomic,strong)NSMutableArray *shoseArray;
@property (nonatomic,strong)NSMutableArray *shoseCycleArray;
@end

@implementation SneakersController
-(NSMutableArray *)shoseArray {
    if (!_shoseArray) {
        _shoseArray = [[NSMutableArray alloc]init];
    }
    return _shoseArray;
}
-(NSMutableArray *)shoseCycleArray {
    if (!_shoseCycleArray) {
        _shoseCycleArray = [[NSMutableArray alloc]init];
    }
    return _shoseCycleArray;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setViews];
    [self loadDataFromServerStart];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setViews {
    [self.tableView registerNib:[UINib nibWithNibName:@"shoseCell" bundle:nil] forCellReuseIdentifier:@"call"];
    [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
 
    self.cycleView = [[LDTCycleView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 230) InterVal:3];
    [self.tableView addSubview:self.cycleView];
}
-(void)loadDataFromServerStart {
 
    NSData *data =[NSJSONSerialization dataWithJSONObject:@{@"curVersion":@"4.0.4",@"channelId":@"3",@"limit":@12,@"language":@"zh-Hans",@"refreshType":@1,@"localNum":@12,@"lastTime":@"1462265286",@"startTime":@0,@"ruleTime":@"1462265286"}options:NSJSONWritingPrettyPrinted error:nil];
    NSString *params = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSString *parStr = [params stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet decimalDigitCharacterSet]];
    

    
    [LDTNetWorkRequestManager requestURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://new.yohoboys.com/yohoboyins/v4/channel/contentList?parameters=%@",parStr]] Method:LDT_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *dict=jsonDict[@"data"][@"content"];
        for (NSDictionary *dic in dict) {
            shoseModel *model = [shoseModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.shoseArray addObject:model];
        }
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }];
    
    
    NSData *dicData1 = [NSJSONSerialization dataWithJSONObject:@{@"channelId":@"5-19",@"curVersion":@"4.0.4",@"language":@"zh-Hans"} options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *paras1 = [[NSString alloc]initWithData:dicData1 encoding:NSUTF8StringEncoding];
    NSString *parasStr1 = [paras1 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet decimalDigitCharacterSet]];
    
    [LDTNetWorkRequestManager requestURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://new.yohoboys.com/yohoboyins/v4/channel/banner?parameters=%@",parasStr1]] Method:LDT_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array= jsonDict[@"data"];
        
        for (NSDictionary *dic in array) {
            shoseModel *model = [[shoseModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.image]]];
            
            [self.shoseCycleArray addObject:image];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            self.cycleView.imgNameArray = self.shoseCycleArray;
        });
        
    }];

}
#pragma mark - Table view data source


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    shoseDetialController *bfashionVC = [[shoseDetialController alloc]init];
    shoseModel *model = self.shoseArray[indexPath.row];
    
    bfashionVC.publishUrl = model.publishURL;
    bfashionVC.titl = model.title;
    [self.navigationController pushViewController:bfashionVC animated:YES];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.shoseArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    shoseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"call" forIndexPath:indexPath];
    
    cell.model = self.shoseArray[indexPath.row];
    
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)];
    
    tableView.tableHeaderView=headerView;
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 350;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
